#coding=utf-8
#Boa:FramePanel:PanelDeliveryPlan

import wx

from ctrlAider import MyComboBoxDecorator
from product import CDataStaff,CDataTruck
from product import CDataDeliveryPlan,CDataDeliveryState,CDataDeliveryRoute

from MyListCtrl import MyListCtrlRouteDetail,MyListCtrlRegionCustomer,MyListCtrlDeliverySaleOrder
from MyListCtrl import MyOrderContentListCtrl,MyListCtrlDeliveryPlanProDetail

[wxID_PANELDELIVERYPLAN, wxID_PANELDELIVERYPLANBTNCANCEL, 
 wxID_PANELDELIVERYPLANBTNSAVE, wxID_PANELDELIVERYPLANBTNSELROUTE, 
 wxID_PANELDELIVERYPLANCOMBODRIVER, wxID_PANELDELIVERYPLANCOMBOFOLLOWER, 
 wxID_PANELDELIVERYPLANCOMBOSTATE, wxID_PANELDELIVERYPLANCOMBOTRUCK, 
 wxID_PANELDELIVERYPLANDATEPLAN, wxID_PANELDELIVERYPLANDATERETURN, 
 wxID_PANELDELIVERYPLANDATESEND, wxID_PANELDELIVERYPLANNOTEBOOK1, 
 wxID_PANELDELIVERYPLANPANEL1, wxID_PANELDELIVERYPLANPANEL2, 
 wxID_PANELDELIVERYPLANSTATICTEXT1, wxID_PANELDELIVERYPLANTBILLNO, 
 wxID_PANELDELIVERYPLANTCAR, wxID_PANELDELIVERYPLANTCBILLNO, 
 wxID_PANELDELIVERYPLANTCMEMO, wxID_PANELDELIVERYPLANTCROUTE, 
 wxID_PANELDELIVERYPLANTDRIVER, wxID_PANELDELIVERYPLANTFOLLOWER, 
 wxID_PANELDELIVERYPLANTMEMO, wxID_PANELDELIVERYPLANTPLANDATE, 
 wxID_PANELDELIVERYPLANTRETURNDATE, wxID_PANELDELIVERYPLANTSENDDATE, 
 wxID_PANELDELIVERYPLANTSTATE, wxID_PANELDELIVERYPLANTTITLE, 
] = [wx.NewId() for _init_ctrls in range(28)]

class PanelDeliveryPlan(wx.Panel):
    def _init_coll_notebook1_Pages(self, parent):
        # generated method, don't edit

        parent.AddPage(imageId=-1, page=self.panel1, select=True,
              text=u'\u8ba2\u5355\u4e00\u89c8')
        parent.AddPage(imageId=-1, page=self.panel2, select=False,
              text=u'\u968f\u8f66\u4ea7\u54c1\u6e05\u5355')

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELDELIVERYPLAN,
              name=u'PanelDeliveryPlan', parent=prnt, pos=wx.Point(1, 27),
              size=wx.Size(1073, 705), style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(1065, 671))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.tTitle = wx.StaticText(id=wxID_PANELDELIVERYPLANTTITLE,
              label=u'\u76f4\u9001\u8ba1\u5212', name=u'tTitle', parent=self,
              pos=wx.Point(240, 32), size=wx.Size(84, 20), style=0)
        self.tTitle.SetFont(wx.Font(15, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))

        self.tSendDate = wx.StaticText(id=wxID_PANELDELIVERYPLANTSENDDATE,
              label=u'\u51fa\u8f66\u65f6\u95f4\uff1a', name=u'tSendDate',
              parent=self, pos=wx.Point(432, 104), size=wx.Size(85, 16),
              style=0)

        self.dateSend = wx.DatePickerCtrl(id=wxID_PANELDELIVERYPLANDATESEND,
              name=u'dateSend', parent=self, pos=wx.Point(520, 96),
              size=wx.Size(112, 24), style=wx.DP_SHOWCENTURY)

        self.tReturnDate = wx.StaticText(id=wxID_PANELDELIVERYPLANTRETURNDATE,
              label=u'\u6536\u8f66\u65f6\u95f4\uff1a', name=u'tReturnDate',
              parent=self, pos=wx.Point(432, 136), size=wx.Size(85, 16),
              style=0)

        self.dateReturn = wx.DatePickerCtrl(id=wxID_PANELDELIVERYPLANDATERETURN,
              name=u'dateReturn', parent=self, pos=wx.Point(520, 128),
              size=wx.Size(112, 24), style=wx.DP_SHOWCENTURY)

        self.tDriver = wx.StaticText(id=wxID_PANELDELIVERYPLANTDRIVER,
              label=u'\u53f8\u673a\uff1a', name=u'tDriver', parent=self,
              pos=wx.Point(16, 104), size=wx.Size(51, 16), style=0)

        self.tFollower = wx.StaticText(id=wxID_PANELDELIVERYPLANTFOLLOWER,
              label=u'\u8ddf\u8f66\uff1a', name=u'tFollower', parent=self,
              pos=wx.Point(16, 136), size=wx.Size(51, 16), style=0)

        self.staticText1 = wx.StaticText(id=wxID_PANELDELIVERYPLANSTATICTEXT1,
              label=u'\u8def\u7ebf\uff1a', name='staticText1', parent=self,
              pos=wx.Point(16, 72), size=wx.Size(51, 16), style=0)

        self.tcRoute = wx.TextCtrl(id=wxID_PANELDELIVERYPLANTCROUTE,
              name=u'tcRoute', parent=self, pos=wx.Point(72, 64),
              size=wx.Size(264, 26), style=0, value=u'')

        self.btnSelRoute = wx.Button(id=wxID_PANELDELIVERYPLANBTNSELROUTE,
              label=u'+', name=u'btnSelRoute', parent=self, pos=wx.Point(336,
              64), size=wx.Size(24, 26), style=0)
        self.btnSelRoute.Bind(wx.EVT_BUTTON, self.OnBtnSelRouteButton,
              id=wxID_PANELDELIVERYPLANBTNSELROUTE)

        self.notebook1 = wx.Notebook(id=wxID_PANELDELIVERYPLANNOTEBOOK1,
              name='notebook1', parent=self, pos=wx.Point(16, 192),
              size=wx.Size(744, 464), style=0)

        self.panel1 = wx.Panel(id=wxID_PANELDELIVERYPLANPANEL1, name='panel1',
              parent=self.notebook1, pos=wx.Point(0, 0), size=wx.Size(736, 435),
              style=wx.TAB_TRAVERSAL)

        self.panel2 = wx.Panel(id=wxID_PANELDELIVERYPLANPANEL2, name='panel2',
              parent=self.notebook1, pos=wx.Point(0, 0), size=wx.Size(736, 435),
              style=wx.TAB_TRAVERSAL)

        self.tCar = wx.StaticText(id=wxID_PANELDELIVERYPLANTCAR,
              label=u'\u8f66\u8f86\uff1a', name=u'tCar', parent=self,
              pos=wx.Point(200, 104), size=wx.Size(51, 16), style=0)

        self.tPlanDate = wx.StaticText(id=wxID_PANELDELIVERYPLANTPLANDATE,
              label=u'\u8ba1\u5212\u65f6\u95f4\uff1a', name=u'tPlanDate',
              parent=self, pos=wx.Point(432, 72), size=wx.Size(85, 16),
              style=0)

        self.datePlan = wx.DatePickerCtrl(id=wxID_PANELDELIVERYPLANDATEPLAN,
              name=u'datePlan', parent=self, pos=wx.Point(520, 64),
              size=wx.Size(112, 24), style=wx.DP_SHOWCENTURY)
        self.datePlan.Enable(False)

        self.comboTruck = wx.ComboBox(choices=[],
              id=wxID_PANELDELIVERYPLANCOMBOTRUCK, name=u'comboTruck',
              parent=self, pos=wx.Point(256, 96), size=wx.Size(100, 24),
              style=0, value=u'')
        self.comboTruck.SetLabel(u'')

        self.comboDriver = wx.ComboBox(choices=[],
              id=wxID_PANELDELIVERYPLANCOMBODRIVER, name=u'comboDriver',
              parent=self, pos=wx.Point(72, 96), size=wx.Size(100, 24), style=0,
              value=u'')
        self.comboDriver.SetLabel(u'')

        self.comboFollower = wx.ComboBox(choices=[],
              id=wxID_PANELDELIVERYPLANCOMBOFOLLOWER, name=u'comboFollower',
              parent=self, pos=wx.Point(72, 128), size=wx.Size(100, 24),
              style=0, value=u'')
        self.comboFollower.SetLabel(u'')

        self.tState = wx.StaticText(id=wxID_PANELDELIVERYPLANTSTATE,
              label=u'\u72b6\u6001\uff1a', name=u'tState', parent=self,
              pos=wx.Point(200, 136), size=wx.Size(51, 16), style=0)

        self.comboState = wx.ComboBox(choices=[],
              id=wxID_PANELDELIVERYPLANCOMBOSTATE, name=u'comboState',
              parent=self, pos=wx.Point(256, 128), size=wx.Size(100, 24),
              style=0, value=u'')
        self.comboState.SetLabel(u'')

        self.btnSave = wx.Button(id=wxID_PANELDELIVERYPLANBTNSAVE,
              label=u'\u4fdd\u5b58', name=u'btnSave', parent=self,
              pos=wx.Point(432, 160), size=wx.Size(93, 26), style=0)
        self.btnSave.Bind(wx.EVT_BUTTON, self.OnBtnSaveButton,
              id=wxID_PANELDELIVERYPLANBTNSAVE)

        self.btnCancel = wx.Button(id=wxID_PANELDELIVERYPLANBTNCANCEL,
              label=u'\u9000\u51fa', name=u'btnCancel', parent=self,
              pos=wx.Point(536, 160), size=wx.Size(93, 26), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_PANELDELIVERYPLANBTNCANCEL)

        self.tBillNo = wx.StaticText(id=wxID_PANELDELIVERYPLANTBILLNO,
              label=u'\u5355\u53f7\uff1a', name=u'tBillNo', parent=self,
              pos=wx.Point(432, 40), size=wx.Size(51, 16), style=0)

        self.tcBillNo = wx.TextCtrl(id=wxID_PANELDELIVERYPLANTCBILLNO,
              name=u'tcBillNo', parent=self, pos=wx.Point(488, 32),
              size=wx.Size(144, 26), style=0, value=u'')
        self.tcBillNo.SetEditable(False)
        self.tcBillNo.Enable(False)

        self.tMemo = wx.StaticText(id=wxID_PANELDELIVERYPLANTMEMO,
              label=u'\u5907\u6ce8\uff1a', name=u'tMemo', parent=self,
              pos=wx.Point(16, 168), size=wx.Size(51, 16), style=0)

        self.tcMemo = wx.TextCtrl(id=wxID_PANELDELIVERYPLANTCMEMO,
              name=u'tcMemo', parent=self, pos=wx.Point(72, 160),
              size=wx.Size(336, 26), style=0, value=u'')

        self._init_coll_notebook1_Pages(self.notebook1)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.cbTruck = MyComboBoxDecorator(self.comboTruck, CDataTruck, CDataTruck.fi_id)
        self.cbTruck.refresh()
        
        self.cbDriver = MyComboBoxDecorator(self.comboDriver, CDataStaff, CDataStaff.COL_NAME)
        self.cbDriver.refresh()
        
        self.cbFollower = MyComboBoxDecorator(self.comboFollower, CDataStaff, CDataStaff.COL_NAME)
        self.cbFollower.refresh()
        
        self.cbState = MyComboBoxDecorator(self.comboState, CDataDeliveryState, CDataDeliveryState.fi_name)
        self.cbState.refresh()
        self.cbState.SetValue(CDataDeliveryState.getDefState().getValue(CDataDeliveryState.fi_name))
        
        self.lcRouteDetail = MyListCtrlRouteDetail(self)
        self.lcRouteDetail.Move(wx.Point(780, 40))
        self.lcRouteDetail.SetSize(wx.Size(250, 192))
        self.lcRouteDetail.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnRegionItemSelected)

        self.lcCustomer = MyListCtrlRegionCustomer(self)
        self.lcCustomer.Move(wx.Point(780, 248))
        self.lcCustomer.SetSize(wx.Size(250, 384))
        
        self.lcBill = MyListCtrlDeliverySaleOrder(self.panel1)
        self.lcBill.Move(wx.Point(10, 10))
        self.lcBill.SetSize(wx.Size(700, 200))
        self.lcBill.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnBillListItemSelected)
        
        self.lcContent = MyOrderContentListCtrl(self.panel1)
        self.lcContent.Move(wx.Point(10, 220))
        self.lcContent.SetSize(wx.Size(700, 200))
        
        self.lcProList = MyListCtrlDeliveryPlanProDetail(self.panel2)
        self.lcProList.Move(wx.Point(10, 10))
        self.lcProList.SetSize(wx.Size(700, 400))
        
        self.datePlan.SetValue(wx.DateTime.Today())
        self.refreshBillId()
        
        self.m_route = None
        self.m_plan = None

    def refreshBillId(self):
        date = self.datePlan.GetValue().Format('%Y%m%d')
        id = CDataDeliveryPlan.newBillId(date)
        self.tcBillNo.SetValue(id)
        
    def selectRoute(self, routeObj):
        self.m_route = routeObj
        self.tcRoute.SetValue(self.m_route.getValue(self.m_route.fi_name))
        
        self.lcRouteDetail.refresh({'routeId':self.m_route.getValue(self.m_route.fi_id)})
    
    def getPlanId(self):
        return self.tcBillNo.GetValue()
        
    def setEditData(self, planId):
        self.m_plan = CDataDeliveryPlan.findObj(planId)
        
        self.loadBill(self.m_plan)
        
    def doClosePage(self):
        book = self.GetParent()
        book.DeletePage(book.GetSelection())        
    
    def doClose(self, doQuit=True):
        self.doClosePage()
        
    def OnBillListItemSelected(self, event):
        '''按照选中订单，显示订单内容'''
        item = self.lcBill.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        #orderId = self.orderList.GetItem(item, self.orderList.COL_ID).GetText()
        billId = self.lcBill.getBillId(item)
        self.lcContent.refresh({'billId':billId})
        
    def OnRegionItemSelected(self, event):
        item = self.lcRouteDetail.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        regionId = self.lcRouteDetail.getRegionId(item)
        self.lcCustomer.refresh({'regionId':regionId})
        
    def OnBtnSelRouteButton(self, event):
        import DlgRoute
        
        dlg = DlgRoute.create(self)
        if (dlg.ShowModal() != wx.ID_OK):
            return
        
        self.selectRoute(dlg.getSelectObj())

    def enableAll(self, enable=True):
        self.dateSend.Enable(enable)
        self.dateSend.Enable(enable)
        self.dateReturn.Enable(enable)
        self.comboTruck.Enable(enable)
        self.comboDriver.Enable(enable)
        self.comboFollower.Enable(enable)
        self.comboState.Enable(enable)
        self.tcMemo.Enable(enable)
        self.tcRoute.Enable(enable)
        self.btnSave.Enable(enable)
        
    def loadBill(self, planObj):
        import kit        
        self.tcBillNo.SetValue(planObj.getKeyVal())
        
        self.datePlan.SetValue(kit.getWxDate(planObj.getValue(planObj.fi_planDate)))
        self.dateSend.SetValue(kit.getWxDate(planObj.getValue(planObj.fi_sendDate)))
        self.dateReturn.SetValue(kit.getWxDate(planObj.getValue(planObj.fi_returnDate)))
        self.cbTruck.SetValue(planObj.getTruckName())
        self.cbDriver.SetValue(planObj.getDriverName())
        self.cbFollower.SetValue(planObj.getFollowerName())
        self.cbState.SetValue(planObj.getStateName())
        self.tcMemo.SetValue(planObj.getValue(planObj.fi_memo))
        
        self.lcBill.refresh({'deliveryPlan':planObj.getKeyVal()})
        self.selectRoute(CDataDeliveryRoute.findObj(planObj.getValue(planObj.fi_routeId)))
        self.lcProList.refresh({'deliveryPlan':planObj.getKeyVal()})
        
    def trySave(self):
        import dbmngr
        import kit
        
        data = {}
        data[CDataDeliveryPlan.fi_id] = self.tcBillNo.GetValue()
        data[CDataDeliveryPlan.fi_planDate] = kit.str2Date(self.datePlan.GetValue().Format('%Y-%m-%d'))
        data[CDataDeliveryPlan.fi_sendDate] = kit.str2Date(self.dateSend.GetValue().Format('%Y-%m-%d'))
        data[CDataDeliveryPlan.fi_returnDate] = kit.str2Date(self.dateReturn.GetValue().Format('%Y-%m-%d'))
        
        truck = self.cbTruck.getSelectObj()
        if truck:
            data[CDataDeliveryPlan.fi_truck] = truck.getKeyVal()
            
        driver = self.cbDriver.getSelectObj()
        if driver:
            data[CDataDeliveryPlan.fi_driver] = driver.getKeyVal()
            
        follower = self.cbFollower.getSelectObj()
        if follower:
            data[CDataDeliveryPlan.fi_follower1] = follower.getKeyVal()
            
        #data[CDataDeliveryPlan.fi_follower2] = None
        data[CDataDeliveryPlan.fi_routeId] = self.m_route.getKeyVal()
        
        state = self.cbState.getSelectObj()
        if state:
            data[CDataDeliveryPlan.fi_state] = state.getKeyVal()
        
        data[CDataDeliveryPlan.fi_memo] = self.tcMemo.GetValue()
          
        if self.m_plan:
            self.m_plan.setMulFieldVal(data)
            self.m_plan.update()
        else:
            self.m_plan = CDataDeliveryPlan.factory(False)
            self.m_plan.setMulFieldVal(data)
            ret = self.m_plan.insert()
        
    def OnBtnSaveButton(self, event):
        if not self.m_route:
            dlg = wx.MessageDialog(self, u'数据不完整',u'提示',wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return False
        
        self.trySave()
        self.enableAll(False)


    def OnBtnCancelButton(self, event):
        self.doClosePage()

